import { EventTarget } from 'cc';
import { Logger, ManagerImpl } from '../../../../wing/assets/src';

/**
 * - 作者: DoooReyn
 * - 日期: 2023.10.31
 * - 名称: Manager
 * - 描述: 管理器基类
 */
export abstract class Manager extends EventTarget implements ManagerImpl {
    /** 获取管理器单例 */
    public static GetInstance<T extends Manager>(this: new () => T): T {
        let instance: T = (<any>this).__instance__;
        if (!instance) {
            instance = (<any>this).__instance__ = new this();
        }
        return instance!;
    }

    /** 模块名称 */
    public abstract get uname(): string;

    /** 日志 */
    public get logger() {
        return Logger.getLogger(this.uname);
    }

    /** 是否已初始化 */
    protected _inited: boolean = false;

    /** 是否已初始化 */
    public get inited() {
        return this._inited;
    }

    /** 初始化 */
    public async init(...args: any[]): Promise<void> {
        if (this._inited) return;
        await this.onInit(...args);
        this._inited = true;
    }

    /** 反初始化 */
    public async deinit(): Promise<void> {
        if (!this._inited) return;
        await this.onDeinit();
        this._inited = false;
        Logger.removeLogger(this.uname);
    }

    /** 初始化时调用 */
    protected abstract onInit(...args: any[]): Promise<void>;

    /** 反初始化时调用 */
    protected abstract onDeinit(): Promise<void>;
}
